//
//  HMHomeTableViewController.swift
//  HMWeibo
//
//  Created by heima on 16/4/6.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import SVProgressHUD

private let HomeCellID = "HomeCellID"

class MPHomeController: MPVisitorTableViewController {
    
   private lazy var statusViewModel: MPStatusListViewModel = MPStatusListViewModel()
    
//    //模型数组
//    lazy var statuses: [MPStatus] = [MPStatus]()
    //视图层次结构已经准备完毕
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置界面
        setupUI()
        //加载数据
        loadData()
        
    }
    
    private func setupUI() {
        
        if !userLogin {
        
            //设置访客视图信息
            VisitorView?.setVisitorInfo("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)

           return
        }
        
                //设置导航条上的自定义按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop",target: self, action: "push")
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch",target: self, action: "push")
        
        prepareForTableView()
    }
    
    private func prepareForTableView() {
        //注册cell
        tableView.registerClass(MPStatusCell.self, forCellReuseIdentifier: HomeCellID)
        
        tableView.rowHeight = 250

    }
    
    @objc private func push() {
        //进入子页面
        print("push")
        let temp = MPTempViewController()
        //        temp.hidesBottomBarWhenPushed = true
        //push到子页面
        navigationController?.pushViewController(temp, animated: true)
    }
    
    
    private func loadData() {
        
        statusViewModel.loadHomeData { (isSucceeded) -> () in
            
            if !isSucceeded {
            
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            
            self.tableView.reloadData()
        }
  }
}
    //MARK: 实现数据源 & 协议方法
    extension MPHomeController {
        
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return statusViewModel.statusArray.count
        }
        
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            
            /*
            'unable to dequeue a cell with identifier HomeCellId - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
            必须手动注册cell  xib/sb -> 属性设置框中填写 可重用标示符
            手写代码  手动调用注册cell的方法
            */
            let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as! MPStatusCell

            cell.status = statusViewModel.statusArray[indexPath.row].status
            
            return cell
        }
}



